package org.dingyom.product.controller;

import io.swagger.annotations.ApiOperation;
import org.dingyom.org.domain.Department;
import org.dingyom.product.service.IProductTypeService;
import org.dingyom.product.domain.ProductType;
import org.dingyom.product.query.ProductTypeQuery;
import org.dingyom.basics.util.AjaxResult;
import org.dingyom.basics.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productType")
public class ProductTypeController {
    @Autowired
    public IProductTypeService productTypeService;


    /**
     * 保存和修改公用的
     * @param productType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ProductType productType){
        try {
            if( productType.getId()!=null)
                productTypeService.modify(productType);
            else
                productTypeService.add(productType);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productTypeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            ProductType productType = productTypeService.getOne(id);
            return AjaxResult.me().setData(productType);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< ProductType> list = productTypeService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody ProductTypeQuery query)
    {
        try {
            PageList<ProductType> pageList = productTypeService.getPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }


    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            productTypeService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }

    //获取产品树结构
    @GetMapping("/tree")
    @ApiOperation(value = "部门树结构")//打在方法上 对方法做描述
    public AjaxResult getTree(){
        try {
            List<ProductType> tree = productTypeService.getTree();
            return AjaxResult.me().setData(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }
    //获取父级产品
    @GetMapping("/parent")
    @ApiOperation(value = "部门树结构")//打在方法上 对方法做描述
    public AjaxResult getParent(){
        try {
            List<ProductType> parents = productTypeService.getParent();
            return AjaxResult.me().setData(parents);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }
}
